*************************************                                  ** Forecasting Miltiary Expenditure **                                  ** Tobias Boehmelt & Vincenzo Bove  **                                  ** This version: March 7, 2014      **                                  *************************************use "C:\Users\tbohmelt\Dropbox\Oil and Civil War\Predicting Milex\Nordhaus et al 2012 data\Final Data.dta", clear*************** Data Setup ***************tab STATEdrop if STATE==317drop if STATE==343drop if STATE==344drop if STATE==346drop if STATE==349drop if STATE==359drop if STATE==366drop if STATE==367drop if STATE==368drop if STATE==369drop if STATE==370drop if STATE==371drop if STATE==372drop if STATE==373drop if STATE==581drop if STATE==701drop if STATE==702drop if STATE==703drop if STATE==704drop if STATE==705saveold "C:\Users\tbohmelt\Dropbox\Oil and Civil War\Predicting Milex\Nordhaus et al 2012 data\Final Data 2.dta", replacelabel var LMILEX "Military Spending"label var LMILEX1 "Lagged Dependent Variable"label var PEACEYRS "Peace Years"label var DEMOC "Democracy"label var TRADE_GDP "Trade/GDP"label var CONTIG "Contiguity"label var ALLIES "Allies"label var GDP_WRLD "GDP/World GDP"label var NUMSTATE "Number of States in System"label var LNRGDP "GDP (ln)"label var LNFOES "Spending Foes (ln)"label var LNFRIENDS  "Spending Friends (ln)"******************************* Core Model for Predictions *******************************eststo clearreg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS, reststo Areg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, reststo Breg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)eststo Ccoefplot A B C, bylabel(Model 1) xline(0) drop(_cons) level(90)esttab using "Tab1.tex", drop (_cons) se nocon title(Determinants of National Military Spending) nonumbers label nostar b(%10.3f) r2 nonotes addnotes("Standard errors in parentheses.") replace ************************************ In-Sample Prediction: 1952-2000 ************************************preservereg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)predict yhat if e(sample)twoway (mband LMILEX YEAR) (mband yhat YEAR, lcolor(gs9) lpattern(dash)) if e(sample), ylabel(,labsize(small)) legend(off) scheme(lean1) xtitle("")  ytitle(, size(small)) ytitle("Log of Military Spending")gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if e(sample)quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if e(sample)quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'su suemodlocal MSPE=r(mean)display `MSPE'restore***************************************** Out-of-Sample Prediction: Full Model *****************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}*********************************************** Out-of-Sample Prediction: Without PEACEYRS ***********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}******************************************** Out-of-Sample Prediction: Without DEMOC ********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}************************************************ Out-of-Sample Prediction: Without TRADE_GDP ************************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}********************************************* Out-of-Sample Prediction: Without CONTIG *********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}********************************************* Out-of-Sample Prediction: Without ALLIES *********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}*********************************************** Out-of-Sample Prediction: Without GDP_WRLD ***********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}*********************************************** Out-of-Sample Prediction: Without NUMSTATE ***********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD LNRGDP LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}********************************************* Out-of-Sample Prediction: Without LNRGDP *********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNFOES LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}********************************************* Out-of-Sample Prediction: Without LNFOES *********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFRIENDS LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}************************************************ Out-of-Sample Prediction: Without LNFRIENDS ************************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}********************************************** Out-of-Sample Prediction: Without LMILEX1 **********************************************forvalues h=1/10 {preservequietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX PEACEYRS DEMOC TRADE_GDP CONTIG ALLIES GDP_WRLD NUMSTATE LNRGDP LNFOES LNFRIENDS if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}************************************ Out-of-Sample Prediction Graphs ************************************use "C:\Users\tbohmelt\Dropbox\Oil and Civil War\Predicting Milex\Nordhaus et al 2012 data\OoSGraph.dta", cleartwoway (scatter mean_U sample, sort) if sample<7, scheme(lean1) aspectratio(1)twoway (scatter mean_MSPE sample, sort) if sample<7, scheme(lean1) aspectratio(1)twoway (scatter mean_U sample, sort) if sample<1 | sample>6, scheme(lean1) aspectratio(1)twoway (scatter mean_MSPE sample, sort) if sample<1 | sample>6, scheme(lean1) aspectratio(1)preservecollapse mean_U mean_MSPE referernce_U referernce_MSPE diff_U diff_MSPE sample, by(variables)list sample variables  mean_U mean_MSPE  diff_U diff_MSPErestore************************************ Bayesian Model Averaging (in R) ************************************use "C:\Users\tbohmelt\Dropbox\Oil and Civil War\Predicting Milex\Nordhaus et al 2012 data\Final Data 2.dta", cleardescribesumlibrary(foreign);library(BMS);mydata<-read.dta("C:/Users/tbohmelt/Dropbox/Oil and Civil War/Predicting Milex/Nordhaus et al 2012 data/Final Data 2.dta");summary(mydata);summary(mydata);names(mydata);names(mydata)[5] <- 'Lagged dependent variable';names(mydata)[6] <- 'Peace Years';names(mydata)[7] <- 'Democracy';names(mydata)[8] <- 'Trade/GDP';names(mydata)[9] <- 'Contiguity';names(mydata)[10] <- 'Allies';names(mydata)[11] <- 'GDP/World GDP';names(mydata)[12] <- 'Number of States in System';names(mydata)[13] <- 'GDP (ln)';names(mydata)[14] <- 'Spending Foes (ln)';names(mydata)[15] <- 'Spending Friends (ln)';names(mydata);finaldata=mydata[,-c(1,2,3)];summary(finaldata);rownames(finaldata)=paste(mydata[,"STATE"],mydata[,"YEAR"],sep="_");finaldata=as.matrix(finaldata);head(finaldata);mfls1=bms(finaldata, burn=100000, iter=200000, g="UIP", mprior="fixed", mprior.size=8, mcmc="bd", nmodel=2000);mfls2=bms(finaldata, burn=100000, iter=200000, g="UIP", mprior="random", mprior.size=8, mcmc="bd", nmodel=2000);mfls3=bms(finaldata, burn=100000, iter=200000, g="UIP", mprior="pip", mprior.size=c(.1,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5), mcmc="bd", nmodel=2000);mfls4=bms(finaldata, burn=100000, iter=200000, g="UIP", mprior="uniform", mcmc="bd", nmodel=2000);mfls5=bms(finaldata, burn=100000, iter=200000, g="BRIC", mprior="uniform", mcmc="bd", nmodel=2000);plotComp(Uniform=mfls4, BRIC=mfls5, Fixed=mfls1, PIP=mfls3, Random=mfls2)****************************************** Out-of-Sample Prediction: Final Model ******************************************forvalues h=1/10 {preservequietly reg LMILEX DEMOC NUMSTATE LNRGDP PEACEYRS LNFOES LMILEX1, cluster(STATE)xtile group=uniform() if e(sample), nq(4)drop if group==.gen yhat=.forvalues i=1/4 {quietly reg LMILEX DEMOC NUMSTATE LNRGDP PEACEYRS LNFOES LMILEX1 if group~=`i', cluster(STATE)quietly predict yhat_iquietly replace yhat=yhat_i if group==`i'quietly drop yhat_i}gen suemod=(LMILEX-yhat)^2gen suenaiv=(LMILEX-LMILEX1)^2egen ssuemod=total(suemod) if yhat!=.quietly su ssuemodlocal num=r(mean)egen ssuenaiv=total(suenaiv) if yhat!=.quietly su ssuenaivlocal den=r(mean)local U=sqrt(`num'/`den') display `U'quietly su suemodlocal MSPE=r(mean)display `MSPE'capture drop group yhatrestore}************************************* In-Sample Prediction: By Country *************************************preserveuse "C:\Users\tbohmelt\Dropbox\Oil and Civil War\Predicting Milex\Nordhaus et al 2012 data\Final Data 2.dta", replacetab STATE, gen(ccode_)forval i = 1(1)141{	reg LMILEX DEMOC NUMSTATE LNRGDP PEACEYRS LNFOES LMILEX1 if ccode_`i'==1, r	predict yhat_`i' if e(sample)	gen suemod_`i'=(LMILEX-yhat_`i')^2 if e(sample)	gen suenaiv_`i'=(LMILEX-LMILEX1)^2 if e(sample)	egen ssuemod_`i'=total(suemod_`i') if e(sample)	quietly su ssuemod_`i'	local num=r(mean)	egen ssuenaiv_`i'=total(suenaiv_`i') if e(sample)	quietly su ssuenaiv_`i'	local den=r(mean)	su suemod_`i'	gen MSPE_`i'=r(mean) if e(sample)	drop  yhat_`i' suemod_`i' suenaiv_`i' ssuemod_`i' ssuenaiv_`i'		}mvencode MSPE_1-MSPE_141, mv(0)egen MSPE=rowtotal(MSPE_1-MSPE_141)drop MSPE_1-MSPE_141forval i = 1(1)141{	reg LMILEX DEMOC NUMSTATE LNRGDP PEACEYRS LNFOES LMILEX1 if ccode_`i'==1, r	predict yhat_`i' if e(sample)	gen suemod_`i'=(LMILEX-yhat_`i')^2 if e(sample)	gen suenaiv_`i'=(LMILEX-LMILEX1)^2 if e(sample)	egen ssuemod_`i'=total(suemod_`i') if e(sample)	quietly su ssuemod_`i'	local num=r(mean)	egen ssuenaiv_`i'=total(suenaiv_`i') if e(sample)	quietly su ssuenaiv_`i'	local den=r(mean)	gen U_`i'=sqrt(`num'/`den') if e(sample) 	su suemod_`i'	drop  yhat_`i' suemod_`i' suenaiv_`i' ssuemod_`i' ssuenaiv_`i'		}drop ccode_1-ccode_141mvencode U_1-U_141, mv(0)egen U=rowtotal(U_1-U_141)drop U_1-U_141collapse MSPE U, by(STATE)sort MSPE STATEsort U STATErestore